import {reactive} from 'vue'
import * as API from '@/service/modules/resource'

type Field = {
  value: string
  label: string
}
const OPTION_KEY_MAP:Record<string, Field> = {
  vendor: {
    value: 'vendorss',
    label: 'vendorss'
  },
  modelName: {
    value: 'models_name',
    label: 'models_name'
  },
  room: {
    value: 'idc_name',
    label: 'idc_name'
  },
  role: {
    value: 'net_role_name',
    label: 'net_role_name'
  },
  model: {
    value: 'models',
    label: 'models'
  },
  rack: {
    value: 'rack_names',
    label: 'rack_names'
  },
  netzone: {
    value: 'net_zone_name',
    label: 'net_zone_name'
  },
  category: {
    value: 'category_name',
    label: 'category_name'
  },
  status: {
    value: 'status',
    label: 'status'
  },
  framework: {
    value: 'net_framework_name',
    label: 'net_framework_name'
  },
  attr: {
    value: 'net_attribute_name',
    label: 'net_attribute_name'
  }
}


export const useOption = () => {
  /**查询条件模块选项 */
  const optionList = reactive({
    /**供应商 */
    vendor: [] as any[],
    /**型号 */
    modelName: [] as any[],
    /**机房 */
    room: [] as any[],
    /**模块 */
    model: [] as any[],
    /**区域 */
    netzone: [] as any[],
    /**机柜 */
    rack: [] as any[],
    /**角色 */
    role: [] as any[],
    /**类型 */
    category: [] as any[],
    /**状态 */
    status: [] as any[],
    /**网络架构 */
    framework: [] as any[],
    /**网络属性 */
    attr: [] as any
  } as Record<string, any[]>)

  /**
   * 请求下拉选项
   * @param key
   * @param params
   * @param data
   */
  const queryOptions = async (key: string, params: any, data?:any) => {
    const res:any = await API.queryResourceCondtion(params, data)
    const arr:any[] = res.data || []
    const cfg = OPTION_KEY_MAP[key]
    optionList[key] = arr.map(el => ({
      label: el[cfg.label],
      value: el[cfg.value]
    }))
  }
  
  const getOptionList = () => {
    queryOptions('vendor', {cc: 'vendorss'})
    queryOptions('modelName', {cc: 'models-type'})
    queryOptions('room', {cc: 'datacenterss'})
    // queryOptions('model', {cc: 'models'})
    queryOptions('netzone', {cc: 'net-zone'})
    // queryOptions('rack', {cc: 'rack_name'})
    queryOptions('role', {cc: 'net-role'})
    queryOptions('framework', {cc: 'net-framework'})
    queryOptions('status', {cc: 'status'})
    queryOptions('attr', {cc: 'net-attribute'})
    queryOptions('category', {cc: 'categorys-type'})
  }
  /**
   * 获取模块选项
   * @param data
   */
  const getModelOption = (data: any) => {
    queryOptions('model', {cc: 'models'}, data)
  }

  /**
   * 获取机柜选项
   * @param data
   */
  const getRackOption = (data:any) => {
    queryOptions('rack', {cc: 'rack_names'}, data)
  }
  
  /**
   * 清除选项
   * @param key
   */
  const clearOption = (key: string) => {
    optionList[key] = []
  }

  return {
    getOptionList,
    optionList,
    getModelOption,
    getRackOption,
    clearOption
  }  
}